/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 *
 * https://www.renren.io
 *
 * 版权所有，侵权必究！
 */

package io.renren.modules.app.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sun.org.apache.bcel.internal.generic.NEW;
import io.renren.common.utils.R;
import io.renren.modules.alarm.entity.GuardEntity;
import io.renren.modules.alarm.entity.MachineEntity;
import io.renren.modules.alarm.service.GuardService;
import io.renren.modules.alarm.service.MachineService;
import io.renren.modules.app.annotation.Login;
import io.renren.modules.app.annotation.LoginUser;
import io.renren.modules.app.entity.UserEntity;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * APP测试接口
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("/app")
@Api("APP测试接口")
public class AppTestController {

    @Autowired
    private MachineService machineService;
    @Autowired
    private GuardService guardService;

    @Login
    @GetMapping("userInfo")
    @ApiOperation("获取用户信息")
    public R userInfo(@LoginUser GuardEntity user){
        return R.ok().put("user", user);
    }

    @Login
    @GetMapping("userId")
    @ApiOperation("获取用户ID")
    public R userInfo(@RequestAttribute("userId") Integer userId){
        return R.ok().put("userId", userId);
    }

    @GetMapping("notToken")
    @ApiOperation("忽略Token验证测试")
    public R notToken(){
        return R.ok().put("msg", "无需token也能访问。。。");
    }

    @Login
    @GetMapping("machInfo/{chargeId}")
    @ApiOperation("获得负责的机器信息")
    public R machInfo(@PathVariable("chargeId")Long chargeId){
        List<MachineEntity> machinelist = machineService.list(
                new QueryWrapper<MachineEntity>()
                        .eq("charge_id", chargeId)
                        .or().eq("charge2_id",chargeId));

        return R.ok().put("machines",machinelist);
    }
    @PostMapping("/update")
    @ApiOperation("修改信息")
    public R update(@RequestBody GuardEntity guard) {
        guardService.updateById(guard);
        return R.ok();
    }
}
